package com.jkgh.remedium.objects.messages {
	
	import com.jkgh.logger.Log;
	import com.jkgh.remedium.RemediumConstants;
	import com.jkgh.remedium.RemediumSystem;
	import com.jkgh.remedium.objects.RemediumObject;
	
	import flash.utils.Dictionary;
	
	public class RemediumOutMessage {
		
		private var history:Dictionary/*RemediumObject, Integer*/;
		private var historySize:int;
		
		public function RemediumOutMessage() {
			this.history = new Dictionary();
			this.historySize = 0;
		}
		
		public final function produce(remedium:RemediumSystem):void {
			remedium.consumeByte(getRemediumMessageType());
			produceContent(remedium);
			remedium.flush();
		}
		
		protected function produceContent(remedium:RemediumSystem):void {
			throw new Error();
		}
		
		protected function getRemediumMessageType():int {
			throw new Error();
		}
		
		public final function produceObject(object:RemediumObject, remedium:RemediumSystem):void {
			if (object == null) {
				remedium.consumeByte(RemediumConstants.NULL);
			} else {
				var value:Object = history[object];
				if (value == null) {
					history[object] = historySize++;
					remedium.consumeByte(RemediumConstants.VALUE);
					object.produceContent(remedium, this);
				} else {
					remedium.consumeByte(RemediumConstants.HISTORY);
					remedium.consumeInt(int(value));
				}
			}
		}
	}
}